package org.youchan.aco.p2p;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SessionCommand {
	public enum Type {
		SESSION_START,
		GET_FRIENDS_LIST,
	};
	
	public enum Status {
		SEND,
		RECEIVED,
	}
	
	Type type;
	Status status;
	Map<String, String> values;
	Map<String, List<String>> array;
	
	public SessionCommand(Type type) {
		this.type = type;
		this.values = new HashMap<String, String>();
		this.array = new HashMap<String, List<String>>();
	}

	public Status getStatus() {
		return status;
	}

	public void setStatus(Status status) {
		this.status = status;
	}

	public void setValue(String name, String value) {
		this.values.put(name, value);
	}

	public void addValue(String name, String value) {
		List<String> list = this.array.get(name);
		if (list == null) {
			list = new ArrayList<String>();
			this.array.put(name, list);
		}
		list.add(value);
	}
	
	public String getValue(String name) {
		return this.values.get(name);
	}
	
	public String getValue(String name, int index) {
		return this.array.get(name).get(index);
	}

	public String[] getArray(String name) {
		return this.array.get(name).toArray(new String[0]);
	}
	
	public String outputXML() {
		StringBuffer sb = new StringBuffer("<message type=\"");
		sb.append(type.toString().toLowerCase());
		sb.append("\">");
		for (String name : values.keySet()) {
			sb.append("<value name=\"");
			sb.append(name);
			sb.append("\">");
			sb.append(values.get(name));
			sb.append("</value>");
		}
		for (String name : array.keySet()) {
			sb.append("<array name=\"");
			sb.append(name);
			sb.append("\">");
			for (String value : array.get(name)) {
				sb.append("<value>");
				sb.append(value);
				sb.append("</value>");
			}
			sb.append("</array>");
		}
		sb.append("</message>");
		return sb.toString();
	}

}
